package com.ssm.controller;

import com.ssm.pojo.Lise;
import com.ssm.pojo.OrderForm;
import com.ssm.pojo.User;
import com.ssm.service.OrderFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class OrderFormController {

    @Autowired
    private OrderFormService orderFormService;

    @RequestMapping("addOrderForm.html")
    public String addOrderForm(OrderForm orderForm ,String totalPrice, Integer id, HttpSession session){
        //用户id
        User user = (User) session.getAttribute("SESSION_USER");
        orderForm.setUser(user);
        Lise lise = new Lise();
        lise.setId(id);
        orderForm.setLise(lise);
        //订单号
        orderForm.setOrderNumber("123"+System.currentTimeMillis()+"456");
        orderForm.setStatus(1);

        //切割$
        String total =totalPrice.substring(1);
        orderForm.setTotalPrice(total);
        //订单状态
        orderForm.setSettleAccounts(1);

        //创建订单时间
        Date dt =new Date();
        DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //HH表示24小时制；
        String formatDate = dFormat.format(dt);
        orderForm.setOrderTime(formatDate);
        orderFormService.addOrderForm(orderForm);
        return "leading/cartSuccess";
    }

    @RequestMapping("getOrderForm.html")
    public String getOrderForm(Model model){
//        User user = (User) session.getAttribute("SESSION_USER");

        List<OrderForm> orderForm = orderFormService.getOrderFormBy(1);
        model.addAttribute("order",orderForm);
        return "leading/car";
    }

    @RequestMapping("delectOrderForm.html")
    public String delectOrderForm(Integer id){
        orderFormService.delectOrderForm(id);
        return "forward:getOrderForm.html";
    }

    @RequestMapping("updateOrderForm.html")
    public String updateOrderForm(OrderForm orderForm){
        orderForm.setSettleAccounts(2);
        orderFormService.updateOrderForm(orderForm);
        return "leading/success";
    }

    @RequestMapping("getOrder.html")
    public String getOrder(Model model,HttpSession session){
        User user = (User) session.getAttribute("SESSION_USER");

        List<OrderForm> orderFormList = orderFormService.getOrderForm(user.getId());
        model.addAttribute("orderFormList",orderFormList);
        return  "/user/vipOrder";
    }

}
